#!/usr/bin/perl
use Net::SSH::Expect;
        #
        # You can do SSH authentication with user-password or without it.
        # Making an ssh connection with user-password authentication
        # 1) construct the object
        my $ssh = Net::SSH::Expect->new (
            host => "10.231.20.25", 
            password=> $ARGV[1], 
            user => $ARGV[0], 
            raw_pty => 1
        );
        # 2) logon to the SSH server using those credentials.
        # test the login output to make sure we had success
        my $login_output = $ssh->login();
#        print ($login_output);
      # if ($login_output !~ /Welcome/) {
         if ($login_output !~ /Last login/) {
            die "Login has failed. Login output was $login_output";
        }
       
        # - now you know you're logged in - #

        # disable terminal translations and echo on the SSH server
        # executing on the server the stty command:
        #$ssh->exec("/usr/bin/stty -echo");
        #$ssh->exec("stty raw -echo");
        
       # Now let's run an interactive command, like passwd.
        # This is done combining send() and waitfor() methods together:
        $ssh->send("passwd");
        $ssh->waitfor(':\s*\z', 1) or die "prompt 'password' not found after 1 second";
        $ssh->send($ARGV[1]);
        $ssh->waitfor(':\s*\z', 1) or die "prompt 'New password:' not found";
        $ssh->send($ARGV[2]);
        $ssh->waitfor(':\s*\z', 1) or die "prompt 'Confirm new password:' not found";
        $ssh->send($ARGV[2]);

        # check that we have the system prompt again.
        #my ($before_match, $match) = $ssh->waitfor('\$\s*\z', 1);  # waitfor() in a list context
        #die "passwd failed. passwd said '$before_match'.'$match'" unless ($match);

        # closes the ssh connection
        $ssh->close();
